﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
using MongoDB.Bson;
using SpyApp.Models.BaseCommon.MSG_STR;
using SpyApp.Models.BaseCommon;
using SpyApp.Models.EntytiRepossitory;
using SpyApp.Models.BaseCommon.Enum;

namespace SpyApp.Models
{
    public class SafeSystemContext
    {
        public static bool ServerIsDown = false;   
        private MongoDatabase db;            

        public SafeSystemContext()
        {
            //MongoClientSettings settings = new MongoClientSettings();
            //settings.Server = new MongoServerAddress("mongodb://safesystem:123456@ds047940.mongolab.com:47940/spyapp");            
            //MongoClient client = new MongoClient("mongodb://safesystem:123456@ds047940.mongolab.com:47940/spyapp");
            //MongoClient client = new MongoClient("mongodb://safesystem:123456@ds047940.mongolab.com:47940/spyapp /?socketTimeoutMS=2000;connectTimeoutMS=2000;waitqueuetimeoutms=2000");
        
            MongoClient client = new MongoClient(Properties.Settings.Default.Connection.ToString());
            var server = client.GetServer();                        
            this.db = server.GetDatabase(Properties.Settings.Default.DatabaseName.ToString());
            MongoCollection collection = db.GetCollection<User>("User");
            try
            {
                this.db.Server.Ping();
                ServerIsDown = false;
            }
            catch (Exception ex)
            {
                ServerIsDown = true;
                //if (server.State == MongoServerState.Disconnected)
                //    server.Connect();
            }            

        }

        
     

        public MongoDatabase Database
        {
            get 
            {
                return this.db;
            }
        }

        public string DatabaseName
        {
            get
            {
                return db.Name; 
            }
        }

        public MongoCollection<User> Users
        {
            get
            {                
               return db.GetCollection<User>("User");                
            }        
        }
        
        public MongoCollection<Device> Devices
        {
            get
            {             
               return db.GetCollection<Device>("Device");
            }
        }
        
        public MongoCollection<Message> Messages
        {
            get
            {             
               return db.GetCollection<Message>("Message");
            }
        }

        public MongoCollection<History> History
        {
            get
            {             
                return db.GetCollection<History> ("History");
            }
        }

        public MongoCollection<Roles> Roles
        {
            get
            {
                
                return db.GetCollection<Roles> ("Roles");
            }
        }

        public MongoCollection<UsersInRoles> UserInRoles
        {
            get
            {
                
                return db.GetCollection<UsersInRoles>("UsersInRoles");
            }
        }  
            
    }
}